import axios from "axios";
import { MessageBox, Message } from "element-ui";
import { Loading } from 'element-ui';
const http = axios.create({
    // baseURL: 'http://shiyansong.cn:8888/api/private/v1/',//线上接口
    baseURL:process.env.VUE_APP_API,
    timeout: 10000
})
//请求拦截
http.interceptors.request.use(config => {
    let token = localStorage.getItem('token')
    if (token) {
        config.headers.Authorization = token

    }
    return config

}, error => {
    Promise.reject(error)
})

// 响应拦截
http.interceptors.response.use(res => {
    let code = res.data.meta.status//响应状态码
    if (res.data.meta.msg == '无效token') {
        MessageBox.confirm('token过期,是否跳转到登录页面?', '提示', {
            confirmButtonText: '重新登陆',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            location.href = '/login'
        }).catch(() => {

        })
    }
    if (code === 400 || code === 401 || code === 403 || code === 404 || code === 500) {
        Message.error({
            duration: 1000,
            message: res.data.meta.msg
        })
    }
    return res.data
}, error => {
    console.log('响应错误', error);
    if (Message == "Network Error") {
        Message = "后端接口连接异常";
    }
    else if (Message.includes("timeout")) {
        Message = "系统接口请求超时";
    }
    Message({
        message: Message,
        type: 'error',
        duration: 1000
    })
     return Promise.reject(error)
})
export default http